import tensorflow as tf
import numpy as np


def moving_average(a,w=10):
    a_tensor = tf.convert_to_tensor(a)
    tensor_length = tf.shape(a_tensor)[0]
    if tensor_length <w:
        return a_tensor.numpy()
    result = tf.TensorArray(dtype=a_tensor.dtype,size=tensor_length)
    cum_sum = tf.zeros_like(a_tensor[:w])
    for i in range(w):
        cum_sum += a_tensor[i]
    result = result.write(0,cum_sum/tf.cast(w,a_tensor.dtype))

    for i in range(w,tensor_length):
        cum_sum += a_tensor[i] - a_tensor[i-w]
        result = result.write(i,cum_sum/tf.cast(w,a_tensor.dtype))
    return result.stack().numpy()

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(moving_average(a,w=3))